package com.manxi.controller.admin;

import com.manxi.dto.DishDTO;
import com.manxi.dto.DishPageQueryDTO;
import com.manxi.entity.Dish;
import com.manxi.result.PageResult;
import com.manxi.result.Result;
import com.manxi.service.DishService;
import com.manxi.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DishService dishService;

    /**
     * 新增菜品和对应的口味
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品和对应的口味")
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品和对应的口味：{}", dishDTO);
        dishService.saveWithFlavor(dishDTO);

        //清除redis缓存
        String key = "dish_" + dishDTO.getCategoryId();
        cleanCache(key);

        return Result.success();
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询:{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("菜品id:{}", id);
        DishVO dishVO = dishService.getById(id);
        return Result.success(dishVO);
    }

    /**
     * 批量删除菜品和删除单个菜品
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品和删除单个菜品")
    public Result<String> delete(@RequestParam List<Long> ids) {
        log.info("菜品id:{}", ids);
        dishService.deleteBatch(ids);

        //清除redis缓存
        cleanCache("dish_*");

        return Result.success();
    }

    /**
     * 更改菜品信息
     *
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("更改菜品信息")
    public Result<String> update(@RequestBody DishDTO dishDTO) {
        log.info("更改菜品信息:{}", dishDTO);
        dishService.updateWithFlavor(dishDTO);
        //清除redis缓存
        cleanCache("dish_*");

        return Result.success();
    }

    /**
     * 启售禁售菜品
     *
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启售禁售菜品")
    public Result<String> StartOrStop(@PathVariable Integer status, Long id) {
        log.info("菜品状态和id:{},{}", status, id);
        dishService.StartOrStop(status, id);
        //清除redis缓存
        cleanCache("dish_*");

        return Result.success();
    }


    /**
     * 套餐查询菜品列表
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("套餐查询菜品列表")
    public Result<List<Dish>> list(Long categoryId, String name) {
        log.info("套餐查询菜品列表:{},{}", categoryId, name);
        List<Dish> dishList = dishService.list(categoryId, name);
        return Result.success(dishList);
    }


    /**
     * 清除缓存数据
     *
     * @param pattern
     */
    private void cleanCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
